// pages/mine/mine.js
var date = new Date();
var currentHours = date.getHours();
var currentMinute = date.getMinutes(); /*这三个为全局变量*/
const app = getApp()
const db = wx.cloud.database()
Page({
  data: {
    dis:true,
    sum: " ",
    carts: [], // 购物车列表
    store:[],
    addressList: [],
    t:false,
    bz:{},
  },
  onLoad: function (options) {
    var openId = wx.getStorageSync('openId');
    db.collection('ztd').where({
      _openid: openId,
      selected:true
    })
    .get()
    .then(res => {
      if(res.data.length == 0){
        console.log('为空')
        wx.showToast({
          title: '无自提点跳转中',
          duration: 2000,
          icon: 'error'
        })    
  setTimeout(()=>{
        wx.navigateTo({
          url: '/pages/store/store'
      })},2000
  )
  }
      setTimeout(() => {
       let one = res.data.splice(0,1);
        this.setData({
          store: one
        })
      }, 200)
    });

    var openId = wx.getStorageSync('openId');
    var all = JSON.parse(options.id);
    let total = 0;
    for (let i = 0; i < all.length; i++) {
       total += all[i].num * all[i].price; // 所有价格加起来
     }
    // 再通过setData更改Page()里面的data，动态更新页面的数据
    let p1 = new Promise((resolve, reject) => {
      db.collection('address').where({
        _openid: openId,
        'str.selected': true
      }).get().then(res => {
        var t = res.data;
        let tL = t.length;
        if(tL===0){
          wx.showToast({
            title: '无收货人员！',
            duration: 1000,
            icon: 'error'
          })
          this.setData({
            t:true
          })
        }else{
        var a = t[0].str;
        resolve(a);
        }
      })
    })
    Promise.all([p1]).then((result) => {
      var t = result[0];
      this.setData({
        addressList:result,
        carts:all,
        sum:total.toFixed(2)
      })
      //加入KEY为组合属性
      const carts = this.data.carts.map(item => {
        item.key = `${item.name}-${item.guage}`;
        return item;
      });
    }) 
  
  },
  pay: function () {
    let bz = this.data.store;
    let bzVa = bz[0].name
    let perlist = this.data.addressList;
    let pername = perlist[0].name;
    let pernum = perlist[0].num;
    let allStr = bzVa + '/' + pername + '/'+ pernum;
    var carts = this.data.carts;
    let alldata = [];
    //接下来是通过循环将数据存到向表格中存数据的数组中*/
    for (let i = 0; i < carts.length; i++) {
      let arr = [];
      arr.push(carts[i].name + carts[i].guage);
      arr.push(carts[i].num);
      alldata.push(arr);
      var alldatastr = alldata.toString();
    }
    var buytime = new Date();
    var summoney = this.data.sum
    if (summoney > 0) {
      wx.showToast({
        title: '准备结算......',
        icon: "error",
        duration: 3000
      })
      var sum = this.data.sum
      var openId = wx.getStorageSync('openId');
      db.collection('dsbsaled').add({
        data: {
          buytime: buytime,
          sum: sum,
          list: alldatastr,
          isqb: '未取',
          allStr:allStr,
          order:true
        },
        success: function () {
        },
        fail: function () {
          wx.showToast({
            title: '下单失败！查网络',
          })
        }
      })
    }
  },
  bz:function(e){
    var bz = this.data.bz;
    bz.value = e.detail.value;
  }
})